package com.zhkj.txg.module.member.ui

import android.view.View
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.zhkj.lib.base.ViewModelActivity
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.txg.R
import com.zhkj.txg.module.member.adapter.MemberIncomeAdapter
import com.zhkj.txg.module.member.entity.RebateStatisticsEntity
import com.zhkj.txg.module.member.vm.MemberListViewModel
import kotlinx.android.synthetic.main.activity_member_income.*

class MemberIncomeActivity : ViewModelActivity<MemberListViewModel>() {
    override fun setViewModel(): MemberListViewModel = createViewModel()
    override fun statusTitleBar(): View? = titleIncome
    override val layoutResId: Int = R.layout.activity_member_income

    private val incomeAdapter = MemberIncomeAdapter()

    override fun initUI() {
        with(rvIncome) {
            layoutManager = LinearLayoutManager(this@MemberIncomeActivity)
            adapter = incomeAdapter
            addItemDecoration(DividerItemDecoration(this@MemberIncomeActivity, DividerItemDecoration.VERTICAL).apply {
                val drawable = ContextCompat.getDrawable(this@MemberIncomeActivity, R.drawable.divider_vertical_1)
                if (drawable != null) setDrawable(drawable)
            })
        }
        incomeAdapter.setEmptyView(R.layout.layout_empty_data)
    }

    override fun initData() {
        viewModel.rebateStatistics()
    }

    override fun initViewModel() {
        viewModel.rebateStatisticsLiveData.observe(this, Observer { response ->
            handlerResponseStatus(response.status, response.statusTip)
            if (response.status == HttpResponseStatus.Status.SUCCESS) response.response?.data?.let { data ->
                val list = mutableListOf<RebateStatisticsEntity>().also {
                    if (data.today != null) it.add(data.today)
                    if (data.yesterday != null) it.add(data.yesterday)
                    if (data.month != null) it.add(data.month)
                    if (data.lastMonth != null) it.add(data.lastMonth)
                }
                incomeAdapter.setNewData(list)
            }
        })
    }
}